package com.zyzy.leftfourdead.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zyzy.leftfourdead.R;
import com.zyzy.leftfourdead.adapter.ProductActivityAdapter.ProductListViewInstructionAdapter;
import com.zyzy.leftfourdead.adapter.ProductActivityAdapter.ProductListViewListAdapter;
import com.zyzy.leftfourdead.adapter.ProductActivityAdapter.ProductViewPagerAdapter;
import com.zyzy.leftfourdead.bean.PreductBean;
import com.zyzy.leftfourdead.helper.MyListView;
import com.zyzy.leftfourdead.helper.OkHttpClientHelper;
import com.zyzy.leftfourdead.helper.UrlHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class ProductActivity extends AppCompatActivity {
    private static final String TAG = "ProductActivity";
    private Context context=this;
    private String key=null;
    private List<PreductBean.InstructionBean> list=new ArrayList<>();
    private List<String> images=new ArrayList<>();
    private List<String> description=new ArrayList<>();
    private ViewPager viewPager;
    private TextView textView_product_name;
    private TextView textView_product_subtitle;
    private TextView textView_product_vipprice;
    private TextView textView_product_country;
    private TextView textView_product_delivery_mode_name;
    private TextView textView_product_promotion;
    private MyListView listView_product_list;
    private MyListView listView_product_description;
    private MyListView listView_product_instruction;
    private Handler handler=new Handler(); 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product);

        initData();
        
        initView();
    }

    private void initView() {
        viewPager= (ViewPager) findViewById(R.id.viewPager_product);
        textView_product_name= (TextView) findViewById(R.id.textView_product_name);
        textView_product_subtitle= (TextView) findViewById(R.id.textView_product_subtitle);
        textView_product_vipprice= (TextView) findViewById(R.id.textView_product_vipprice);
        textView_product_country= (TextView) findViewById(R.id.textView_product_country);
        textView_product_delivery_mode_name= (TextView) findViewById(R.id.textView_product_delivery_mode_name);
        textView_product_promotion= (TextView) findViewById(R.id.textView_product_promotion);
        listView_product_list= (MyListView) findViewById(R.id.listView_product_list);
        listView_product_description= (MyListView) findViewById(R.id.listView_product_description);
        listView_product_instruction= (MyListView) findViewById(R.id.listView_product_instruction);
    }

    private void initData() {
        Intent intent=getIntent();
        Bundle bundle = intent.getExtras();
        key=bundle.getString("sku");

        loadNetworkData();
    }

    //    加载网络数据
    public void loadNetworkData() {
        final String urlString = "http://as-vip.missfresh.cn/v3/product/"+key+"?&access_token=&version=4.3.1";
//        开启子线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String json = OkHttpClientHelper.getStringFromURL(context, urlString, "PRODUCT");
                    Gson gson = new Gson();
                    final PreductBean bean = gson.fromJson(json, new TypeToken<PreductBean>() {
                    }.getType());
                    description = bean.getDescription();
                    images = bean.getImages();

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            initAdapter(bean);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initAdapter(PreductBean bean) {
        ProductViewPagerAdapter viewPagerAdapter=new ProductViewPagerAdapter(context,images);
        viewPager.setAdapter(viewPagerAdapter);

        textView_product_name.setText(bean.getName());
        textView_product_subtitle.setText(bean.getSubtitle());
        float price=((float)bean.getVip_price())/100;
        textView_product_vipprice.setText("¥"+price);
        textView_product_country.setText(bean.getCountry());
        textView_product_delivery_mode_name.setText(bean.getDelivery_mode_name());
        textView_product_promotion.setText(bean.getPromotion().get(0));

        List<Map<String,String>> stringlist=new ArrayList<Map<String, String>>();
        if (bean.getBrand()!=null){
            Map<String,String> map=new HashMap<String, String>();
            map.put("brand",bean.getBrand().toString());
            stringlist.add(map);
        }
        if (bean.getWeight()!=null){
            Map<String,String> map=new HashMap<String, String>();
            map.put("weight",bean.getWeight());
            stringlist.add(map);
        }
        if (bean.getPack()!=null){
            Map<String,String> map=new HashMap<String, String>();
            map.put("pack",bean.getPack());
            stringlist.add(map);
        }
        if (bean.getStorage_time()!=null){
            Map<String,String> map=new HashMap<String, String>();
            map.put("storage_time",bean.getStorage_time());
            stringlist.add(map);
        }
        if (bean.getStorage_method()!=null){
            Map<String,String> map=new HashMap<String, String>();
            map.put("storage_method",bean.getStorage_method());
            stringlist.add(map);
        }
        Log.i(TAG, "(((((((((stringlist: "+stringlist.toString());
        ProductListViewListAdapter productListViewListAdapter=new ProductListViewListAdapter(context,stringlist);
        listView_product_list.setAdapter(productListViewListAdapter);

        String[] text=new String[description.size()];
        for (int i = 0; i < description.size(); i++) {
            text[i]=description.get(i);
            Log.i(TAG, "(((((((((text: "+text[i]);
        }
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(context,android.R.layout.simple_list_item_1,text);
        listView_product_description.setAdapter(arrayAdapter);

        list=bean.getInstruction();
        if (list!=null&&list.size()!=0){
            Log.i(TAG, "(((((((((list: "+list.toString());
            ProductListViewInstructionAdapter instructionAdapter=new ProductListViewInstructionAdapter(context,list);
            listView_product_instruction.setAdapter(instructionAdapter);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpClientHelper.cancelCall("PRODUCT");
    }
}
